package za.co.smartmi.util;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.*;



/**
 * Basic database helper methods
 */
public class DatabaseHelper implements Serializable {


	/**
	 * Helper Method: Gets a string value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return a string object
	 * @throws java.sql.SQLException
	 */
	public static String getString(ResultSet rs, int column) throws SQLException {
		String value = rs.getString(column);
		if (rs.wasNull() || value == null) {
			return null;
		}
		return value.trim();
	}

	/**
	 * Helper Method: Gets an integer value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return an integer object
	 * @throws java.sql.SQLException
	 */
	public static Integer getInteger(ResultSet rs, int column) throws SQLException {
		int value = rs.getInt(column);
		if (rs.wasNull()) {
			return null;
		}
		return new Integer(value);
	}


	/**
	 * Helper Method: Gets a big decimal value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return a big decimal object
	 * @throws java.sql.SQLException
	 */
	public static BigDecimal getBigDecimal(ResultSet rs, int column) throws SQLException {
		BigDecimal value = rs.getBigDecimal(column);
		if (rs.wasNull()) {
			return null;
		}
		return value;
	}


	/**
	 * Helper Method: Gets an integer value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return an integer object
	 * @throws java.sql.SQLException
	 */
	public static Short getShort(ResultSet rs, int column) throws SQLException {
		short value = rs.getShort(column);
		if (rs.wasNull()) {
			return null;
		}
		return new Short(value);
	}

	/**
	 * Helper Method: Gets a float value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return a float object
	 * @throws java.sql.SQLException
	 */
	public static Float getFloat(ResultSet rs, int column) throws SQLException {
		float value = rs.getFloat(column);
		if (rs.wasNull()) {
			return null;
		}
		return new Float(value);
	}

	/**
	 * Helper Method: Gets a Long value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return a Long object or null if column is SQL Null
	 * @throws java.sql.SQLException
	 */
	public static Long getLong(ResultSet rs, int column) throws SQLException {
		long value = rs.getLong(column);
		if (rs.wasNull()) {
			return null;
		}
		return new Long(value);
	}

	/**
	 * Helper Method: Gets a Timestamp value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return a timestamp object
	 * @throws java.sql.SQLException
	 */
	public static Timestamp getTimestamp(ResultSet rs, int column) throws SQLException {
		Timestamp value = rs.getTimestamp(column);
		if (rs.wasNull() || value == null) {
			return null;
		}
		return value;
	}

	/**
	 * Helper Method: Sets a string value into the preparedStatement object. Taking Care of nulls.
	 * @param ps the prepared statement
	 * @param column the column id
	 * @param value the value to be set.
	 * @throws java.sql.SQLException
	 */
	public static void setString(PreparedStatement ps, int column, String value) throws SQLException {
		ps.setString(column, value );
	}

	/**
	 * Helper Method: Sets an integer value into the prepared statement object.
	 * @param ps the prepared statement
	 * @param column the column id.
	 * @param value the value to be set.
	 * @throws java.sql.SQLException
	 */
	public static void setInteger(PreparedStatement ps, int column, Integer value) throws SQLException {
		if (value == null) {
			ps.setNull(column, Types.INTEGER);
		} else {
			ps.setInt(column, value.intValue());
		}
	}

	/**
	 * Helper Method: Sets a short value into the prepared statement object.
	 * @param ps the prepared statement
	 * @param column the column id.
	 * @param value the value to be set.
	 * @throws java.sql.SQLException
	 */
	public static void setShort(PreparedStatement ps, int column, Short value) throws SQLException {
		if (value == null) {
			ps.setNull(column, Types.INTEGER);
		} else {
			ps.setInt(column, value.intValue());
		}
	}

	/**
	 * Helper Method: Sets a Float value into the prepared statement object.
	 * @param ps the prepared statement
	 * @param column the column id.
	 * @param value the value to be set.
	 * @throws java.sql.SQLException
	 */
	public static void setFloat(PreparedStatement ps, int column, Float value) throws SQLException {
		if (value == null) {
			ps.setNull(column, Types.FLOAT);
		} else {
			ps.setFloat(column, value.floatValue());
		}
	}

	/**
	 * Helper Method: Sets a Long value into the prepared statement object.
	 * @param ps the prepared statement
	 * @param column the column id.
	 * @param value the value to be set.
	 * @throws java.sql.SQLException
	 */
	public static void setLong(PreparedStatement ps, int column, Long value) throws SQLException {
		if (value == null) {
			ps.setNull(column, Types.NUMERIC);
		} else {
			ps.setLong(column, value.longValue());
		}
	}

	/**
	 * Helper Method: Sets a Long value into the prepared statement object.
	 * @param ps the prepared statement
	 * @param column the column id.
	 * @param value the value to be set.
	 * @throws java.sql.SQLException
	 */
	public static void setTimestamp(PreparedStatement ps, int column, Timestamp value) throws SQLException {
		if (value == null) {
			ps.setNull(column, Types.TIMESTAMP);
		} else {
			ps.setTimestamp(column, value);
		}
	}

	/**
	 * Helper Method: Gets a string value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return a string object
	 * @throws java.sql.SQLException
	 */
	public static String getString(ResultSet rs, String column) throws SQLException {
		String value = rs.getString(column);
		if (rs.wasNull() || value == null) {
			return null;
		}
		return value.trim();
	}

	/**
	 * Helper Method: Gets an integer value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return an integer object
	 * @throws java.sql.SQLException
	 */
	public static Integer getInteger(ResultSet rs, String column) throws SQLException {
		int value = rs.getInt(column);
		if (rs.wasNull()) {
			return null;
		}
		return new Integer(value);
	}

	/**
	 * Helper Method: Gets a big decimal value from the resultset object.
	 * @param rs the result set
	 * @param column the column name
	 * @return a big decimal object
	 * @throws java.sql.SQLException
	 */
	public static BigDecimal getBigDecimal(ResultSet rs, String column) throws SQLException {
		BigDecimal value = rs.getBigDecimal(column);
		if (rs.wasNull()) {
			return null;
		}
		return value;
	}


	/**
	 * Helper Method: Gets an integer value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return an integer object
	 * @throws java.sql.SQLException
	 */
	public static Short getShort(ResultSet rs, String column) throws SQLException {
		short value = rs.getShort(column);
		if (rs.wasNull()) {
			return null;
		}
		return new Short(value);
	}

	/**
	 * Helper Method: Gets a float value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return a float object
	 * @throws java.sql.SQLException
	 */
	public static Float getFloat(ResultSet rs, String column) throws SQLException {
		float value = rs.getFloat(column);
		if (rs.wasNull()) {
			return null;
		}
		return new Float(value);
	}

	/**
	 * Helper Method: Gets a Long value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return a Long object or null if column is SQL Null
	 * @throws java.sql.SQLException
	 */
	public static Long getLong(ResultSet rs, String column) throws SQLException {
		long value = rs.getLong(column);
		if (rs.wasNull()) {
			return null;
		}
		return new Long(value);
	}

	/**
	 * Helper Method: Gets a Timestamp value from the resultset object.
	 * @param rs the result set
	 * @param column the column id
	 * @return a timestamp object
	 * @throws java.sql.SQLException
	 */
	public static Timestamp getTimestamp(ResultSet rs, String column) throws SQLException {
		Timestamp value = rs.getTimestamp(column);
		if (rs.wasNull() || value == null) {
			return null;
		}
		return value;
	}
    
    /**
     * Helper Method: Gets a Date value from the resultset object.
     * @param rs the result set
     * @param column the column id
     * @return a date object
     * @throws java.sql.SQLException
     */
    public static Date getDate(ResultSet rs, String column) throws SQLException {
        Date value = rs.getDate(column);
        if (rs.wasNull() || value == null) {
            return null;
        }
        return value;
    }
    
    public static boolean getBooleanPrimitive(ResultSet rs, String column) throws SQLException {
    	String value = rs.getString(column);
    	if (rs.wasNull() || value == null) {
    		 return false;
    	}
    	return "Y".equals(value);
    }
    
    public static Object getObject(ResultSet rs, String column, String columnClassName) throws SQLException {
    	Class columnClass = null;
		try {
			columnClass = Class.forName(columnClassName);
		} catch (ClassNotFoundException e) {
}
		
		if (columnClass != null) {
	    	if (String.class.isAssignableFrom(columnClass)) {
	    		return getString(rs, column);
	    	} else if (Integer.class.isAssignableFrom(columnClass)) {
	    		return getInteger(rs, column);
	    	} else if (Long.class.isAssignableFrom(columnClass)) {
	    		return getLong(rs, column);
	    	} else if (Timestamp.class.isAssignableFrom(columnClass)) {
	    		return getTimestamp(rs, column);
	    	} else if (Short.class.isAssignableFrom(columnClass)) {
	    		return getShort(rs, column);
	    	} else if (BigDecimal.class.isAssignableFrom(columnClass)) {
	    		return getBigDecimal(rs, column);
	    	} else if (Float.class.isAssignableFrom(columnClass)) {
	    		return getFloat(rs, column);
	    	} else if (Date.class.isAssignableFrom(columnClass)) {
	    		return getDate(rs, column);
	    	}
		}
		
		Object value = rs.getObject(column);
		if (rs.wasNull()) {
			return null;
		}
		return value;
	}
}
